
// 新增todo
function toSubmit(){
    //这里面可以做表单提交前的验证判断
    var url = "https://zhangshirui.cn/api/admin/todolist/todo";
    // var url = "http://127.0.0.1:8092/api/admin/todolist/todo";
    var content = document.getElementById("addTodoInput").value;
    if (content == null || content === "") {
        console.log("content == null");
        return;
    }
    var getLocalData = localStorage.getItem("halo__Access-Token");
    //['value']['access_token']
    var jsonObj = JSON.parse(getLocalData);
    if (jsonObj == null || jsonObj.value == null) {
        alert("无操作权限");

        return;
    }
    var token = jsonObj.value.access_token;
    console.log("token:" , token);
    $.ajax({
        url:url,
        type:"POST",
        data: JSON.stringify({'desc':content}),
        headers: {
            'Admin-Authorization':token
        },
        contentType:"application/json; charset=utf-8",
        dataType:"json",
        success: function(response){
            alert("添加成功");
            console.log("test1");
            $("#todolistul").load(location.href+" #todolistul>*","");
        },
        error: function (response) {
            alert("创建待办事项失败")
        }
    })
}

// 删除todo
function delTodo(todoId) {
    var url = "https://zhangshirui.cn/api/admin/todolist/todo";
    // var url = "http://127.0.0.1:8092/api/admin/todolist/todo";
    var getLocalData = localStorage.getItem("halo__Access-Token");

    //['value']['access_token']
    var jsonObj = JSON.parse(getLocalData);
    if (jsonObj == null || jsonObj.value == null) {
        alert("无操作权限");
        return;
    }
    var token = jsonObj.value.access_token;
    console.log("token: ", token);
    console.log("todoId: ", todoId);
    $.ajax({
        url:url,
        type:"PATCH",
        data: JSON.stringify({'id':todoId, 'todoState':3}),
        headers: {
            'Admin-Authorization':token
        },
        contentType:"application/json; charset=utf-8",
        dataType:"json",
        success: function(response){
            console.log("删除成功,刷新页面");
            $("#todolistul").load(location.href+" #todolistul>*","");
        },
        error: function (response) {
            alert("删除todo失败")
        }
    })
}

// checkbox
function changeStatus(obj, id) {
    var url = "https://zhangshirui.cn/api/admin/todolist/todo";
    // var url = "http://127.0.0.1:8092/api/admin/todolist/todo";
    var state = 0;
    var getLocalData = localStorage.getItem("halo__Access-Token");

    //['value']['access_token']
    var jsonObj = JSON.parse(getLocalData);
    if (jsonObj == null || jsonObj.value == null) {
        obj.checked = 0;
        alert("无操作权限");
        return;
    }
    var token = jsonObj.value.access_token;
    console.log("token: ", token);
    console.log(obj.checked)
    if (obj.checked) {
        state = 1;
    }
    console.log("state: ", state);
    $.ajax({
        url:url,
        type:"PATCH",
        data: JSON.stringify({'id':id, 'todoState':state}),
        headers: {
            'Admin-Authorization':token
        },
        contentType:"application/json; charset=utf-8",
        dataType:"json",
        success: function(response){
            console.log("更改状态成功,刷新页面");
            $("#todolistul").load(location.href+" #todolistul>*","");
        },
        error: function (response) {
            alert("更改todo状态失败")
        }
    })
}
